package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.WithdrawQuery;
import cn.org.eshow.common.ShopDataVar;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Money;
import cn.org.eshow.model.Notification;
import cn.org.eshow.model.User;
import cn.org.eshow.model.Withdraw;
import cn.org.eshow.service.BillManager;
import cn.org.eshow.service.MoneyManager;
import cn.org.eshow.service.NotificationManager;
import cn.org.eshow.service.WithdrawManager;
import cn.org.eshow.util.PageUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Results({@Result(name = "input", location = "/add"),
        @Result(name = "list", type = "redirect", location = "/withdraw/"),
        @Result(name = "success", type = "redirect", location = "/view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"search","list", "delete", "view", "state"})
public class WithdrawAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private WithdrawManager withdrawManager;
    @Autowired
    private MoneyManager moneyManager;
    @Autowired
    private BillManager billManager;

    @Autowired
    private NotificationManager notificationManager;

    private List<Withdraw> withdraws;
    private Withdraw withdraw = new Withdraw();
    private WithdrawQuery query = new WithdrawQuery();

    public String search() {
        Page<Withdraw> page = withdrawManager.search(query);
        withdraws = page.getDataList();
        saveRequest("page", PageUtil.conversion(page));
        saveRequest("query", query);
        return LIST;
    }

    public String list() {
        withdraws = withdrawManager.list(query);
        return LIST;
    }

    public String view() {
        if (id != null) {
            withdraw = withdrawManager.get(id);
        } else {
            return INDEX;
        }
        return NONE;
    }

    public String check() {
        withdraw = withdrawManager.check(query);
        return LIST;
    }

    /**
     * 修改提现状态
     *
     * @return
     */
    public String state() {
        if (getSessionUser() == null && withdraw.getState() == null) {
            return LIST;
        }
        Withdraw old = withdrawManager.get(id);
        if (old == null) {
            return LIST;
        }
        old.setState(withdraw.getState());
        old = withdrawManager.save(old);

        User user=old.getUser();
        Money money=moneyManager.mine(user);
        String content = "";
        if (old.getState() == 1) {///提现成功
            moneyManager.withdrawing(money,-old.getPrice());
            moneyManager.withdraw(money,old.getPrice());

            content = "您申请的提现审核成功";
            saveMessage("申请的提现审核成功");
        }
        if (old.getState() == -1) { //提现失败
            moneyManager.withdrawing(money,-old.getPrice());
            moneyManager.balance(money,old.getPrice());
            billManager.calc(user, ShopDataVar.billCategory.用户.toString(),ShopDataVar.billType.提现退款.提现退款.toString(),old.getPrice(),0.0,money.getBalance(),"withdraw",old.getNo(),"提现失败",1,"");
            content = "您申请的提现审核失败，请重新申请";
            saveMessage("申请的提现审核失败");
        }
        notificationManager.save(user,Notification.Category.系统消息.toString(), "提现审核通知", content,"","withdraw", old.getId().toString());

        //推送
        return REDIRECT;
    }

    public List<Withdraw> getWithdraws() {
        return withdraws;
    }

    public void setWithdraws(List<Withdraw> withdraws) {
        this.withdraws = withdraws;
    }

    public Withdraw getWithdraw() {
        return withdraw;
    }

    public void setWithdraw(Withdraw withdraw) {
        this.withdraw = withdraw;
    }

    public WithdrawQuery getQuery() {
        return query;
    }

    public void setQuery(WithdrawQuery query) {
        this.query = query;
    }
}